package com.hujiang.basic.search.index;

import org.elasticsearch.action.bulk.BulkResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.hujiang.basic.framework.rest.validation.annotation.NotBlank;
import com.hujiang.basic.search.api.IndexService;
import com.hujiang.basic.search.common.component.HJElasticSearchClient;
import com.hujiang.basic.search.model.HJBulkDeleteRequest;
import com.hujiang.basic.search.model.HJBulkIndexRequest;
import com.hujiang.basic.search.model.HJDeleteRequest;
import com.hujiang.basic.search.model.HJIndexRequest;
import com.hujiang.basic.search.model.HJResponse;
import com.hujiang.basic.search.model.HJUpdateRequest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api("文档增删改")
@RequestMapping(value = "/v1/doc")
public class IndexController {

    @Autowired
    private HJElasticSearchClient client;

    @Autowired
    private IndexService service;

    @ApiOperation(value = "创建文档", httpMethod = "POST", response = HJResponse.class, notes = "创建文档")
    @RequestMapping(method = RequestMethod.POST)
    @NotBlank({ "req.index", "req.type" })
    public HJResponse create(@RequestBody HJIndexRequest req) {
        return HJResponse.ok(service.add(req));
    }

    @ApiOperation(value = "删除文档", httpMethod = "DELETE", response = HJResponse.class, notes = "删除文档")
    @RequestMapping(method = RequestMethod.DELETE)
    @NotBlank({ "req.index", "req.type", "req.id" })
    public HJResponse delete(@RequestBody HJDeleteRequest req) {
        return HJResponse.ok(service.delete(req));
    }

    @ApiOperation(value = "更新文档", httpMethod = "PUT", response = HJResponse.class, notes = "更新文档")
    @RequestMapping(method = RequestMethod.PUT)
    @NotBlank({ "req.index", "req.type", "req.id" })
    public HJResponse update(@RequestBody HJUpdateRequest req) {
        return HJResponse.ok(service.update(req));
    }

    @ApiOperation(value = "批量创建文档", httpMethod = "POST", response = BulkResponse.class, notes = "批量创建文档")
    @RequestMapping(value = "/bulk", method = RequestMethod.POST)
    @NotBlank({ "req.index", "req.type" })
    public HJResponse bulkAdd(@RequestBody HJBulkIndexRequest req) {
        return HJResponse.ok(service.bulkAdd(req));
    }

    @ApiOperation(value = "批量删除文档", httpMethod = "DELETE", response = BulkResponse.class, notes = "批量删除文档")
    @RequestMapping(value = "/bulk", method = RequestMethod.DELETE)
    @NotBlank({ "req.index", "req.type" })
    public HJResponse bulkDelete(@RequestBody HJBulkDeleteRequest req) {
        return HJResponse.ok(service.bulkDelete(req));
    }

    @ApiOperation(value = "删除老索引", httpMethod = "GET", response = HJResponse.class, notes = "删除老索引")
    @RequestMapping(value = "/deleteOldIndices",method = RequestMethod.GET)
    public HJResponse deleteOldIndices() {
        return HJResponse.ok(service.deleteOldIndices());
    }
}
